/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vistas;

import entidades.Funcionalidad;
import entidades.ProveedorRepuesto;
import entidades.Repuesto;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import rn.RepuestoRN;
import static vistas.Principal.usuario;

/**
 *
 * @author Pablo
 */
public class Proveedores extends javax.swing.JFrame {

    /**
     * Creates new form Proveedores
     */
    
    private static DefaultTableModel modelo_de_tabla;
    private static Repuestos formRepuestos;
    List<Funcionalidad> func_hab;
    
    public Proveedores(Repuestos formRepuesto) {
        initComponents();
        func_hab = usuario.getFuncionalidads();
        bloquearFunciones();
        
        this.formRepuestos = formRepuesto;
        cargarTabla();
        
        this.setLocationRelativeTo(null);
        this.setVisible(true);
       
    }

   
    private static void iniciarModeloTabla() {
        Object datos[][] = {};
        String[] columnNames = {"Id", "Descripcion", "Domicilio"};
        modelo_de_tabla = new DefaultTableModel(datos, columnNames);        
    }
    
    private static void cargarTabla() {
        iniciarModeloTabla();
        List<ProveedorRepuesto> resultado = RepuestoRN.getTodosProveedorRepuesto();
        for (ProveedorRepuesto c : resultado) {
            String fila[] = new String[]{
                c.getId().toString(), c.getDescripcion(), c.getDireccion()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla.setModel(modelo_de_tabla);
    }
    
    public static void actualizarTabla(){
        cargarTabla();
//        if(formRepuestos!=null){
//            formRepuestos.combo_proveedores.removeAllItems();
//            formRepuestos.llenarCombo(formRepuestos.combo_proveedores, RepuestoRN.getTodosProveedorRepuesto());
//        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bt_nuevo = new javax.swing.JButton();
        bt_editar = new javax.swing.JButton();
        bt_eliminar = new javax.swing.JButton();
        bt_imprimir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        bt_salir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Proveedores");

        bt_nuevo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_nuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        bt_nuevo.setText("Nuevo");
        bt_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_nuevoActionPerformed(evt);
            }
        });

        bt_editar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_editar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil.png"))); // NOI18N
        bt_editar.setText("Editar");
        bt_editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_editarActionPerformed(evt);
            }
        });

        bt_eliminar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_eliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        bt_eliminar.setText("Eliminar");
        bt_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_eliminarActionPerformed(evt);
            }
        });

        bt_imprimir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_imprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/printer.png"))); // NOI18N
        bt_imprimir.setText("Imprimir Lista");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bt_nuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bt_editar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bt_eliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bt_imprimir)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_nuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_editar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_eliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_imprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tabla);

        bt_salir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_salir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_salir.setText("Salir");
        bt_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_salirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bt_salir, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bt_salir, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_nuevoActionPerformed
        NuevoProveedor n = new NuevoProveedor(this, "Nuevo Proveedor", null);
        n.setVisible(true);
        
    }//GEN-LAST:event_bt_nuevoActionPerformed

    private void bt_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_eliminarActionPerformed
        if(tabla.getSelectedRow() != -1){
            int selectedRow = tabla.getSelectedRow();
            String id = (String)tabla.getModel().getValueAt(selectedRow, 0);
            Long id_long = new Long(id);
            System.out.println("ID selected: " + id_long);
            int op = JOptionPane.showConfirmDialog(null, "¿Eliminar proveedor?");
            if(op == 0){
                RepuestoRN.eliminarProveedor(id_long);
                cargarTabla();
                if(formRepuestos!=null){
//                    formRepuestos.combo_proveedores.removeAllItems();
//                    formRepuestos.llenarCombo(formRepuestos.combo_proveedores, RepuestoRN.getTodosProveedorRepuesto());
                }
            }
        }
        else
           JOptionPane.showMessageDialog(null, "Debe seleccionar una fila");
    }//GEN-LAST:event_bt_eliminarActionPerformed

    private void bt_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_salirActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_salirActionPerformed

    private void bt_editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_editarActionPerformed
        if(tabla.getSelectedRow() != -1){
            int selectedRow = tabla.getSelectedRow();
            String id = (String)tabla.getModel().getValueAt(selectedRow, 0);
            Long id_long = new Long(id);            
            ProveedorRepuesto prove = RepuestoRN.findProveedorRepuestoPorID(id_long);
            NuevoProveedor n = new NuevoProveedor(this, "Modificar Proveedor", prove);
            n.setVisible(true);
        }
        else
           JOptionPane.showMessageDialog(null, "Debe seleccionar una fila");
    }//GEN-LAST:event_bt_editarActionPerformed

   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_editar;
    private javax.swing.JButton bt_eliminar;
    private javax.swing.JButton bt_imprimir;
    private javax.swing.JButton bt_nuevo;
    private javax.swing.JButton bt_salir;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables

    private boolean estaHabilitado(String func) {
        for (Funcionalidad f : func_hab) {
            if (f.getNombre().equals(func)) {
                return true;
            }
        }
        return false;
    }

    private void bloquearFunciones() {
        if (!estaHabilitado("Proveedores ABM")) {
            bt_editar.setEnabled(false);
            bt_eliminar.setEnabled(false);
            bt_nuevo.setEnabled(false);            
        }
    }
}
